package cn.zwk.common.model;

import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结构体
 *
 * @author haoxr
 * @since 2022/1/30
 **/
@Data
public class Result<T> implements Serializable {

    private Integer code;

    private T data;

    private String msg;

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMsg("操作成功");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failed() {
        return result(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"未知异常，请联系管理员", null);
    }

    public static <T> Result<T> failed(String msg) {
        return result(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, null);
    }

    public static <T> Result<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }

    private static <T> Result<T> result(Integer code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
}
